package com.huitong.common.core.config;

import jakarta.validation.Validator;
import org.hibernate.validator.HibernateValidator;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

import java.util.Properties;

/**
 * 校验框架配置
 *
 * @author fromdrowning
 * @date 2024/3/16 10:45
 */
@AutoConfiguration
public class ValidatorConfiguration {

    /**
     * 校验框架配置
     *
     * @return 校验框架
     */
    @Bean
    public Validator validatorCustomizer() {
        try (LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean()) {
            // 设置使用HibernateValidator校验器并设置快速异常返回
            factoryBean.setProviderClass(HibernateValidator.class);
            Properties properties = new Properties();
            properties.setProperty("hibernate.validator.fail_fast", "true");
            factoryBean.setValidationProperties(properties);
            factoryBean.afterPropertiesSet();
            return factoryBean.getValidator();
        }
    }
}
